
function message(object,info,colorBy){
    object.innerText= info;
    object.style.color=colorBy;
}
function checkNick(){ 
    nickUser=document.getElementById("nickUser").value;
    if(nickUser==null||nickUser==""||nickUser.length>10){
        message(document.getElementById("m02"),"昵称长度在0~10之间","red");
        return false;
    }else{
        message(document.getElementById("m02"),"昵称可以使用","green");
        return true;
    }
}
function checkIntro(){
    userIntro=document.getElementById("userIntro").value;
    if(userIntro==null||userIntro==""||userIntro.length>299){
        message(document.getElementById("m03"),"个性签名长度在0~299之间","red");
    }else{
        message(document.getElementById("m03"),"个性签名可以使用","green");
    }
}
function checkEmail(){
    email = document.getElementById("email").value;
    if(email==null||email==""){
        message(document.getElementById("m04"),"email不能为空","red");
    }else if(email.indexOf("@")==-1||email.length>20){
        message(document.getElementById("m04"),"email格式不正确","red");
    }else{
        message(document.getElementById("m04"),"email可以使用","green");
    }
}
function checkPhone(){
    phone = document.getElementById("phone").value;
    if(phone!=isNaN&&phone.length==11){
        message(document.getElementById("m05"),"电话号码格式正确","green");
    }else{
        message(document.getElementById("m05"),"电话号码格式应是11位纯数字","red");
    }
}
function checkUserPassword(){
    userPassword = document.getElementById("userPassword").value;
    if(userPassword==null||userPassword==""||userPassword.length>15){
        message(document.getElementById("m06"),"密码格式不正确","red");
    }else{
        message(document.getElementById("m06"),"密码可以使用","green");
    }
}
function checkAgain(){
    userPassword = document.getElementById("userPassword").value;
    userPasswordAgain = document.getElementById("userPasswordAgain").value;
    if(userPassword!=""&&userPasswordAgain!=null&&userPassword!=""&&userPassword==userPasswordAgain){
        message(document.getElementById("m07"),"可以使用","green");
    }else{
        message(document.getElementById("m07"),"两次输入密码不一致","red");
    }
}
window.onload=function(){
    let vm = new Vue({
        el:"#register",
        data: {
           user:{
               userName:"",
               nickUser:"",
               userIntro:"",
               email:"",
               phone:"",
               userPassword: ""
           },
           message:"",
           userPasswordAgain:""
        },
        methods: {
            findUserName(){
                axios
                .get("http://localhost:9090/findUserName",{
                    params:{
                        userNameCheck:this.user.userName
                    }
                })
                .then((result=>{
                    console.log(result);
                    if(result.data==0){
                        message(document.getElementById("m01"),"用户名可以使用！","green");
                    }else{
                        message(document.getElementById("m01"),"用户名已被占用！","red");
                    }
                }))
            },
            register(){
                if(document.getElementById("userName").value==null||document.getElementById("userName").value==""||
                document.getElementById("nickUser").value==null||document.getElementById("nickUser").value==""||
                document.getElementById("userIntro").value==null||document.getElementById("userIntro").value==""||
                document.getElementById("email").value==null||document.getElementById("email").value==""||
                document.getElementById("phone").value==null||document.getElementById("phone").value==""||
                document.getElementById("userPassword").value==null||document.getElementById("userPassword").value==""||
                document.getElementById("userPasswordAgain").value==null||document.getElementById("userPasswordAgain").value==""){
                    alert("请填写未填写的数据！");
                    return false;
                }
                if(document.getElementById("m01").style.color=="red"||
                document.getElementById("m02").style.color=="red"||
                document.getElementById("m03").style.color=="red"||
                document.getElementById("m04").style.color=="red"||
                document.getElementById("m05").style.color=="red"||
                document.getElementById("m06").style.color=="red"||
                document.getElementById("m07").style.color=="red"){
                    alert("请输入正确的数据！");
                    return false;
                }
                axios
                .post("http://localhost:9090/register",this.user)
                .then((result)=>{
                    console.log(result);
                    window.location.href="login-register.html"
                })
                .catch((err)=>{
                    console.log(err);
                });
            },
        }
    },
    )
}